﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.DataModel.Users;
using NPMonitor.Implementation;

namespace NPMonitor.Web.UserControls
{
    public partial class User : System.Web.UI.UserControl
    {
        #region 属性

        public int UserId
        {
            get { return Convert.ToInt32(hfId.Value); }
            set { hfId.Value = value.ToString(); }
        }

        public string UserName 
        { 
            get { return txtUserName.Text; }
            set { txtUserName.Text = value; } 
        }

        public string Account 
        {
            get { return txtAccount.Text; }
            set { txtAccount.Text = value; } 
        }

        public string Mail 
        {
            get { return txtUserEmail.Text; }
            set { txtUserEmail.Text = value; } 
        }

        public string status 
        { 
            get { return ddlStatus.SelectedValue; } 
            set { ddlStatus.SelectedValue = value; }
        }

        public UserManagement UserManagentInvoke
        {
            get { return this.Page as UserManagement; }
        }
        #endregion

        #region 方法
        protected void Page_Load(object sender, EventArgs e)
        {
            UserManagentInvoke.PhEdit = false;
        }

        protected override void OnPreRender(EventArgs e)
        {
            rfvAccount.ErrorMessage = string.Format(ValidationString.FieldRequired, "Account");
            cvAccountCount.ErrorMessage = string.Format(ValidationString.FieldLengthExceeded, "Account", "30");
            cvAccountName.ErrorMessage = string.Format(ValidationString.FieldAlreadyExists, "Account");

            rfvUserName.ErrorMessage = string.Format(ValidationString.FieldRequired, "Username");
            cvUserNameCount.ErrorMessage = string.Format(ValidationString.FieldLengthExceeded, "Username", "30");
            cvUserName.ErrorMessage = string.Format(ValidationString.FieldAlreadyExists, "Username");

            rfvEmail.ErrorMessage = string.Format(ValidationString.FieldRequired, "Email");
            cvEmailCount.ErrorMessage = string.Format(ValidationString.FieldLengthExceeded, "Email", "30");
            revfEmail.ErrorMessage = string.Format(ValidationString.EmailTransform);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Users user = new Users();
            user.UserId = UserId;
            user.UserName = UserName;
            user.Mail = Mail;
            user.Account = Account;
            user.Status = status;
            StoreManager.Current.UserStore.SaveUser(UserId, user);
            UserManagentInvoke.UserDataBind(-1);
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "saveSuccessShow", "saveSuccessShow();", true);  
        }

        #endregion
    }
}